package utils;

import java.util.Date;

/**
 * @author yangzhi
 * @date 2018/9/6
 */
public class PageUtils {

	/**
	 * 截取过长字符 按字节截取
	 * @param inputStr
	 * @param len
	 * @param appendStr
	 * @param removeHtml 是否要去除HTML标记
	 * @return
	 */
	public static String stringLeft(String inputStr, int len, String appendStr, Boolean removeHtml) {

		if (Utils.isNullOrEmpty(inputStr) || len <= 0) {
			return "";
		}

		if (removeHtml) {
			inputStr =Utils.removeHtml(inputStr);
		}

		inputStr = inputStr.trim();
		if (Utils.isNullOrEmpty(appendStr)) {
			appendStr = "";
		}
		int leftLen = len - appendStr.length();
		int countLen = 0;
		//字符串长度
		int charCount = 0;

		char[] cArray = inputStr.toCharArray();
		for (char c : cArray) {
			if (c >= 0 && c < 255) {
				countLen++;
			} else {
				countLen += 2;
			}
			if (countLen > leftLen) {
				break;
			}
			charCount++;
		}
		if (charCount == inputStr.length()) {
			return inputStr; // 不需要裁剪
		} else {
			return inputStr.substring(0, charCount) + appendStr;
		}
	}

	public static void main(String[] args){
		//String txt="月黑风高夜，哈哈哈还好还好我想要一辆自行车，红红火火恍恍惚惚红红火火恍恍惚惚红红火火恍恍惚惚";
		String txt="测试问大卡\n";
		Boolean result = isTakeUp(txt,160);
		System.out.println(result);
	}

	public static Boolean isTakeUp(String text,Integer leftLength){
		Boolean result = false;
		char[] cArray = text.trim().toCharArray();
		//字符长度
		Integer countLength=0;
		Integer charCount = 0;
		for (char c : cArray) {
			if (c >= 0 && c < 255) {
				countLength++;
			} else {
				countLength += 2;
			}
			if (countLength > leftLength) {
				break;
			}
			charCount++;
		}
		if (charCount == text.trim().length()) {
			result =false;
		} else {
			result =true;
		}
		return  result;
	}

	/**
	 * 秒转》时：分：秒
	 * @param time
	 * @return
	 */
	public static String secToTime(int time) {
		String timeStr = null;
		int hour = 0;
		int minute = 0;
		//int second = 0;
		if (time <= 0) {
			return "00:00";
		}
		else {
			minute = time / 60;
			if (minute < 60) {
				//second = time % 60;
				timeStr = unitFormat(minute) + "点" ; //unitFormat(second);
			} else {
				hour = minute / 60;
				if (hour > 99) {
					return "99:59:59";
				}
				minute = minute % 60;
				//second = time - hour * 3600 - minute * 60;
				timeStr = unitFormat(hour) + "点" + unitFormat(minute) +"分"; //":" + unitFormat(second);
			}
		}
		return timeStr;
	}

	private static String unitFormat(int i) {
		String retStr = null;
		if (i >= 0 && i < 10) {
			retStr = "0" + Integer.toString(i);
		}
		else {
			retStr = "" + i;
		}
		return retStr;
	}

	/**
	 * 返回ago时间
	 * @param strData
	 * @return
	 */
	public static String getFormatDate(String strData){

		Date date=DateUtils.formatStrToDate(strData, "yyyy-MM-dd HH:mm:ss");
		// 定义最终返回的结果字符串。
		String interval = RelativeDateFormat.format(date);
		return interval;
	}

}
